package br.usp.ime.dojo.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class PilotListWidget extends Composite {

	private final int REFRESH_TIME = 1000;
	private String pilot;
	private String copilot;
	private Button togglePilotQueueBTN;
	private DojoRoomServiceAsync dojoRoomService;
	private FlexTable pilotQueue;
	private UserDTO user;
	private DojoRoomDTO dojoRoom;
	private VerticalPanel vPanel;
	private DojoRoomServiceAsync roomInfoAsync;

	public PilotListWidget(UserDTO userDto, DojoRoomDTO dojoRoomDto){

		roomInfoAsync = GWT.create(DojoRoomService.class);
		user = userDto;
		dojoRoom = dojoRoomDto;
		pilotQueue = new FlexTable();
		pilot = new String("none");
		copilot = new String("none");
		togglePilotQueueBTN = new Button("Join Pilot Queue");
		togglePilotQueueBTN.addClickListener(new ClickListener(){
			public void onClick(Widget sender) {
				if(!user.getName().equals(getPilot()) && !user.getName().equals(getCopilot())) 
					joinPilotQueue();	
			}
		});

		vPanel = new VerticalPanel();
		vPanel.add(pilotQueue);
		vPanel.add(togglePilotQueueBTN);
		initWidget(vPanel);
		
		Timer timer = new Timer(){
			public void run(){
				updatePilotQueueFromServerAndUpdateInterfaceToClient();
				updatePilotFromServer();
				updateCopilotFromServer();
			}
		};
		timer.scheduleRepeating(REFRESH_TIME);
		
	}
	
	private void joinPilotQueue() {
		if(dojoRoomService == null){
			dojoRoomService = GWT.create(DojoRoomService.class);
		}
		
		AsyncCallback<Void > callback = new AsyncCallback<Void>(){
			public void onFailure(Throwable caught) {
			}
			public void onSuccess(Void result) {
			}
		};
		dojoRoomService.toggle(dojoRoom, user, callback);
	}
	
	private void updatePilotFromServer(){
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {

			}

			public void onSuccess(String result) {				
				pilot = result;		
			};
	};
		roomInfoAsync.getPilot(dojoRoom.getName(), callback);
	}
	
	private void updateCopilotFromServer(){
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {

			}

			public void onSuccess(String result) {				
				copilot = result;		
			};
	};
		roomInfoAsync.getCopilot(dojoRoom.getName(), callback);
	}
	
	private void updatePilotQueueFromServerAndUpdateInterfaceToClient(){
		AsyncCallback<List<String> > callback = new AsyncCallback<List<String> >() {
			public void onFailure(Throwable caught) {

			}

			public void onSuccess(List<String> result) {								
				pilotQueue.clear();
				while(pilotQueue.getRowCount() > 0)
					pilotQueue.removeRow(0);
				
				pilotQueue.setText(0, 1, "Pilot Queue:");
				//It is not good to use the "none" name for a null pilot
				//if(!pilot.equals("none")) {
					Label pilotLabel = new Label(pilot);
					pilotLabel.addStyleName("dojoPilotName");
					
					Label copilotLabel = new Label(copilot);
					copilotLabel.addStyleName("dojoPilotName");
					
					pilotQueue.setWidget(1, 0, new Image("images/helmeticon.png"));
					pilotQueue.setWidget(1, 1, pilotLabel);
					
					pilotQueue.setWidget(2, 0, new Image("images/compassicon.png"));
					pilotQueue.setWidget(2, 1, copilotLabel);
					
				//}			
				if(result != null){
					int i = 3;
					for (String username : result) {
						pilotQueue.setText(i++, 1, username);
					}
				}
			}
		};
		roomInfoAsync.getPilotQueueFromRoom(dojoRoom.getName(), callback);
	}
	
	public String getPilot() {
		return pilot;
	}
	
	public String getCopilot() {
		return copilot;
	}
	
}
